class ThumbnailsController < ApplicationController
  require 'RMagick'
  
  skip_before_filter :login_required

  def index
    safe_params = ['action', 'controller', 'image', 'w', 'h', 'sizing', 'enlarge']

    for param, param_value in params
      if !safe_params.include?(param)
        raise "#{param} (#{param_value}) is not a valid parameter"
      end
    end
    sizing = params[:sizing]
    w = params[:w].to_i
    h = params[:h].to_i
    image = params[:image]
    enlarge = params[:enlarge] ||= false

    full_image_path = "#{RAILS_ROOT}/public/images/#{image}"
    
    thumb = Thumbnail.new(full_image_path)
    thumb.sizing = sizing
    thumb.width = w
    thumb.height = h
    
    #render :text => thumb.src_image.r_image.methods.to_yaml.to_s
    #render :text => thumb.src_image.r_image.properties.to_yaml.to_s
    #render :text => thumb.get_all_cached_files.to_yaml.to_s
    #render :text => thumb.has_cache?.to_s
    #return

    send_to_browser thumb
  end
  
  def send_to_browser(thumb)
    send_data thumb.to_blob, :filename => thumb.filename, 
                             :disposition => 'inline', 
                             :type => thumb.mime_type
  end

end
